﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Kaiser.Extensions;
using Kaiser.Modules.Core.Aggregates;
using Kaiser.Modules.Core.Aggregates.Bills;

namespace Kaiser.Modules.Core.Services
{
    /// <summary>
    /// 销售服务类
    /// </summary>
    public class SaleService : Kaiser.Services.ServiceBase<SaleBills>
    {
        public static void AddSale(SaleBills model)
        {

        }


        #region 发送货物

        public class AssembleProductService
        {
             SaleOrderBills item;
            IList<ProductSendObj> list ;
            public  AssembleProductService(SaleOrderBills item, IList<ProductSendObj> list)
            {
            }

            public void Sale()
            {
                var model = ConvertOrderSaleBillToSaleBill(item, list);
                CoreServiceFactory.CreateSaleService().Add(model);
            }

            private SaleBills ConvertOrderSaleBillToSaleBill(SaleOrderBills order, IEnumerable<ProductSendObj> items)
            {
                var model = SaleBills.Create();
                model.SettleAcct = order.SettleAcct;
                model.SaleOrder = order;
                model.BillCode = CoreServiceFactory.CreateBillCodeService().GenerationBillCode(BillCodeService.BillCodeType.销售单);
                model.SaleBillItems.AddRange(ConvertToBillItemses(items));
                return model;
            }

            private IEnumerable<SaleBillItems> ConvertToBillItemses(IEnumerable<ProductSendObj> items)
            {
                var list = new List<SaleBillItems>();

                foreach (var saleBillItemse in items)
                {
                    var billItem = saleBillItemse.SaleOrderBillItems;
                    list.Add(new SaleBillItems()
                    {
                        Product = billItem.Product,
                        Unit = billItem.Unit,
                        ShowItemCount = billItem.ShowItemCount,
                        ItemCount = billItem.ItemCount,
                        Amount = billItem.Amount,
                        CreateDateTime = billItem.CreateDateTime

                        // Amount = saleBillItemse.SaleOrderBillItems
                    });
                }

                return new ArraySegment<SaleBillItems>();
            }

        }

        /// <summary>
        /// 配送服务
        /// </summary>
        /// <param name="item"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public AssembleProductService AssembleProduct(SaleOrderBills item, IList<ProductSendObj> list)
        {
            return new AssembleProductService(item,list);
        }


        /// <summary>
        /// 发货处理
        /// </summary>
        /// <param name="item"></param>
        /// <param name="list">需要发货的商品</param>
        public void SendSaleOrderProduct(SaleOrderBills item, IList<ProductSendObj> list)
        {
         
        }
                


        #endregion
    }
}
